package com.cc.dao;

import org.apache.log4j.Logger;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class BaseDao {
    private static final Logger logger = Logger.getLogger(BaseDao.class);
    private static  String DRIVER = null; //驱动
    private static  String URL = null;  //数据库连接地址
    private static  String USERNAME = null; //用户名
    private static  String PASSWORD = null; //密码

    Connection conn = null; //数据库连接对象
    PreparedStatement pstmt = null; //指令操作对象
    ResultSet rs = null; //结果集
    //静态代码块
    static {
        String driver = "database.properties";
        InputStream in = BaseDao.class.getClassLoader().getResourceAsStream(driver);
        Properties properties = new Properties();
        try {
            properties.load(in);
            DRIVER = properties.getProperty("driver");
            URL = properties.getProperty("url");
            USERNAME = properties.getProperty("username");
            PASSWORD = properties.getProperty("password");
        } catch (IOException e) {
            e.printStackTrace();
        }
        //加载驱动
        try {
            Class.forName(DRIVER);
            logger.debug("驱动加载成功!");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    //获取数据库连接
    private Connection getConnection() {
        try {
            conn = DriverManager.getConnection(URL,USERNAME,PASSWORD);
            logger.debug("数据库连接成功");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
    //执行增删改方法
    public int executeUpdate(String sql,Object... params) {
        //打开连接
        conn = getConnection();
        try {
            logger.debug("增删改操作指令"+sql);
            pstmt  = conn.prepareStatement(sql);
            if (params != null) {
                for (int i = 0; i < params.length; i++) {
                    pstmt .setObject(i+1,params[i]);
                }
            }
            return pstmt .executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closedAll(null);
        }
        return -1;
    }
    //执行查询方法
    public ResultSet executeQuery(String sql,Object... params) {
        conn = getConnection();
        try {
            logger.debug("执行查询指令："+sql);
            pstmt = conn.prepareStatement(sql);
            if (params != null) {
                for (int i = 0; i < params.length; i++) {
                    pstmt.setObject(i+1,params[i]);
                }
            }
            rs = pstmt.executeQuery();
            return rs;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    //关闭连接
    public void closedAll(ResultSet rs){
        try {
            if(rs!=null){
                rs.close();
            }
            if(pstmt!=null){
                pstmt.close();
            }
            if (conn!=null){
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        logger.debug("连接关闭成功");
    }
}
